import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class TestWeb {

    @Test
    public void startSelenium(){
        WebDriver wd = new ChromeDriver();
        wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        wd.get("https://ceshiren.com/");
        wd.findElement(By.cssSelector("#search-button")).click();
        wd.findElement(By.cssSelector("#search-term")).sendKeys("selenium");
    }
    @Test
    public void testGetCookie() throws IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        webDriver.get("https://work.weixin.qq.com/wework_admin/frame");
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Set<Cookie> cookies = webDriver.manage().getCookies();
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        mapper.writeValue(new File("cookies.yaml"),cookies);
    }

    @Test
    public void testLogin() throws Exception {
        WebDriver webDriver = new ChromeDriver();
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webDriver.get("https://work.weixin.qq.com/wework_admin/frame");
        TypeReference<List<HashMap<String, Object>>> typeReference = new TypeReference<List<HashMap<String, Object>>>() {
        };
        List<HashMap<String, Object>> cookies = mapper.readValue(new File("cookies.yaml"), typeReference);
        cookies.forEach(cookieMap -> webDriver.manage().addCookie(new Cookie(cookieMap.get("name").toString(), cookieMap.get("value").toString())));
        webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        webDriver.navigate().refresh();
        //放大窗口
        webDriver.manage().window().maximize();

        //点击通讯录
        webDriver.findElement(By.xpath("//*[@id='menu_contacts']")).click();
        //给予加载页面等待时间，不做任何事
        Thread.sleep(3000);
        //显示等待5秒（5秒内找不到指定元素则抛出异常，作用于元素、页面加载，非全局设置）
        WebDriverWait webDriverWait = new WebDriverWait(webDriver,5);

        //添加成员
        WebElement memberElement = webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='ww_operationBar']/a[@class='qui_btn ww_btn js_add_member']")));
        memberElement.click();

        //填写姓名、英文名、账号、性别、手机
        webDriver.findElement(By.xpath("//*[@id='username']")).sendKeys("刘祺");
        webDriver.findElement(By.xpath("//*[@id='memberAdd_english_name']")).sendKeys("Seven");
        webDriver.findElement(By.xpath("//*[@id='memberAdd_acctid']")).sendKeys("20181818");
        WebElement genderElement = webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@class='member_edit_item member_edit_item_Radios']/div[@class='member_edit_item_right']/label[2]")));
        Thread.sleep(3000);
        genderElement.click();
        webDriver.findElement(By.xpath("//*[@class='ww_telInput']/input[@name='mobile']")).sendKeys("13512345678");

        //点击保存
        WebElement save = webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@class='qui_btn ww_btn js_btn_save']")));
        Thread.sleep(3000);
        save.click();

    }

}
